using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace labDict.Web.common
{
    public class DataGridTemplate : ITemplate
    {
        ListItemType templateType;
        string columnName;
        string dataItemName;

        public enum InputType
        { 
            Label = 0,
            TextBox = 1,
        }

        public DataGridTemplate(ListItemType type, string colname, string diName)
        {
            AssignValues(type, colname, diName);
        }

        public DataGridTemplate(ListItemType type, string colname)
        {
            AssignValues(type, colname, "");
        }

        private void AssignValues(ListItemType type, string colname, string diName)
        {
            templateType = type;
            columnName = colname;
            dataItemName = diName;
        }

        public void InstantiateIn(System.Web.UI.Control container)
        {
            Literal lc = new Literal();
            switch (templateType)
            {
                case ListItemType.Header:
                    lc.Text = "<B>" + columnName + "</B>";
                    container.Controls.Add(lc);
                    break;
                case ListItemType.Item:
                    //lc.Text = DataBinder.Eval(((DataGridItem)container).DataItem, dataItemName);
                    Label lbl = new Label();
                    lbl.DataBinding += new System.EventHandler(this.BindLabel);
                    container.Controls.Add(lbl);
                    break;
                case ListItemType.EditItem:
                    TextBox tb2 = new TextBox();
                    tb2.DataBinding += new System.EventHandler(this.BindTextBox); 
                    container.Controls.Add(tb2);
                    break;
                case ListItemType.Footer:
                    TextBox tb3 = new TextBox();
                    container.Controls.Add(tb3);
                    break;
            }
       }

       private void BindLabel(object sender, System.EventArgs e)
       {
            Label lbl = (Label)sender;
            GridViewRow container =
            (GridViewRow)lbl.NamingContainer;   
            lbl.Text = (DataBinder.Eval(container.DataItem, dataItemName)).ToString();
       }

       private void BindTextBox(object sender, System.EventArgs e)
       {
            TextBox tb = (TextBox)sender;
            GridViewRow container =
            (GridViewRow)tb.NamingContainer;
            tb.Text = (DataBinder.Eval(container.DataItem, dataItemName)).ToString();
       }
    }
}


